import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ball_lion/entity/pay/coin_detail_entity.dart';
import 'package:ball_lion/resources/colours.dart';
import 'package:ball_lion/resources/styles.dart';
import 'package:ball_lion/server/api/pay_api.dart';
import 'package:ball_lion/utils/date_time_utils.dart';
import 'package:ball_lion/utils/utils.dart';
import 'package:ball_lion/utils/widget_utils.dart';

import 'package:ball_lion/widgets/base/wqs_text.dart';

class MyCoinDetailPage extends StatefulWidget {
  const MyCoinDetailPage({super.key});

  @override
  State<MyCoinDetailPage> createState() => _MyCoinDetailPageState();
}

class _MyCoinDetailPageState extends State<MyCoinDetailPage> {
  String goldOrderId = Get.arguments as String;
  CoinDetailEntity? data;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      getData();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: Styles.appBar(title: QsText("订单详情")),
      backgroundColor: Colours.scaffoldBg,
      body: SingleChildScrollView(
        child: Column(children: [
          const SizedBox(height: 10),
          _header(),
          const SizedBox(height: 10),
          body()
        ]),
      ),
    );
  }

  _header() {
    return Styles.card(
      margin: const EdgeInsets.symmetric(horizontal: 8),
      height: 153,
      width: double.infinity,
      padding: const EdgeInsets.only(top: 30),
      child: Column(children: [
        QsText(data?.title ?? '-', fontSize: 16),
        const SizedBox(height: 20),
        Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            QsText(data?.goldChange ?? '-',
                fontSize: 36, fontWeight: FontWeight.w500),
            const SizedBox(width: 3),
            Padding(
              padding: const EdgeInsets.only(top: 4),
              child: QsText('球币'.changeUnit,
                  fontSize: 20, fontWeight: FontWeight.w500),
            )
          ],
        ),
      ]),
    );
  }

  body() {
    return Styles.card(
      margin: const EdgeInsets.symmetric(horizontal: 8),
      padding: const EdgeInsets.all(8),
      child: Column(
        children: [
          const SizedBox(height: 10),
          _item('当前状态', data?.status ?? '-'),
          _item('交易种类', data?.memo ?? '-'),
          _item(
              '交易时间',
              data?.createTime == null
                  ? '-'
                  : DateTimeUtils.formatDateString(
                      data!.createTime!, 'yyyy-MM-dd HH:mm:ss')),
          _item('交易方式', (data?.payType ?? '-').changeUnit),
          _item('交易单号', data?.goldId ?? '-'),
          _item('金币余额', data?.goldAfter ?? '-'),
          if (data?.description != null)
            _item('备注信息', data?.description ?? '-'),
        ],
      ),
    );
  }

  _item(String title, String detail) {
    return Padding(
      padding: const EdgeInsets.only(bottom: 10),
      child: Row(children: [
        QsText(
          title,
          fontSize: 15,
          color: Colours.greyA9,
        ),
        const SizedBox(width: 28),
        Expanded(child: QsText(detail, fontSize: 15))
      ]),
    );
  }

  getData() async {
    final result = await PayApi.coinDetail(goldOrderId);
    if (result != null) {
      data = result;
      update();
    }
  }
}
